package javacommon.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

public class MapApi {

	public static String getAddressByLocation(String gps){
		
		if(StringUtils.isEmpty(gps)){
			return "";
		}
		
		String[] gpsArray = gps.split(",");
		if(gpsArray.length!=2){
			return "";
		}
		String lat = gpsArray[0];
		String lng = gpsArray[1];
		HttpClient httpclient = new DefaultHttpClient();
		HttpUriRequest request = new HttpGet("http://api.map.baidu.com/geocoder?location="+lng+","+lat+"&output=json&key=94dd3062c185d7defb6e267d9b3b8f74");
		try {
			HttpResponse response = httpclient.execute(request);
			HttpEntity entity = response.getEntity();
			if(entity!=null){
				String content = IOUtils.toString(entity.getContent(),"UTF-8");
				JSONObject ob = new JSONObject(content);
				String address = ob.getJSONObject("result").getString("formatted_address");
				if(StringUtils.isEmpty(address)){
					address = "地址解析失败";
				}
				return address;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	
	public static void main(String[] args) {
//		HttpClient httpclient = new DefaultHttpClient();
//		HttpPost httpost = new HttpPost("http://api.m.jinyinbang.com/login");
//		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//		nvps.add(new BasicNameValuePair("username", "172000000210256"));
//		//nvps.add(new BasicNameValuePair("username", "15651026272"));
//        nvps.add(new BasicNameValuePair("password", "652118"));
//        
//		try {
//	        httpost.setEntity(new UrlEncodedFormEntity(nvps));
//			HttpResponse response = httpclient.execute(httpost);
//			HttpEntity entity = response.getEntity();
//			if(entity!=null){
//				String content = IOUtils.toString(entity.getContent(),"UTF-8");
//				System.out.println(content);
//			}
//		} catch (ClientProtocolException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		} catch (IOException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
//		String address=MapApi.getAddressByLocation("121.452625,31.231694");
//		System.out.println(address);
		MapApi MapApi=new MapApi();
		MapApi.ssss();
	}
	
	private void ssss(){
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httpost = new HttpPost("http://channel.api.duapp.com/rest/2.0/channel/channel");
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//		nvps.add(new BasicNameValuePair("username", "172000000210256"));
//		//nvps.add(new BasicNameValuePair("username", "15651026272"));
//        nvps.add(new BasicNameValuePair("password", "652118"));
        
        nvps.add(new BasicNameValuePair("method", "push_msg"));
        nvps.add(new BasicNameValuePair("apikey", "tQKR9urlkglxRLbF5KQk0BGn"));
        nvps.add(new BasicNameValuePair("sign",   "nQy72PGtt33GO9Oh33kRacRIqerAtGgh"));
        										   
        //nvps.add(new BasicNameValuePair("user_id", ""));
        nvps.add(new BasicNameValuePair("push_type", "2"));
        //nvps.add(new BasicNameValuePair("channel_id", ""));
        nvps.add(new BasicNameValuePair("tag", "21232"));//公司id
        //nvps.add(new BasicNameValuePair("device_type", ""));
        //nvps.add(new BasicNameValuePair("message_type", "0"));
        nvps.add(new BasicNameValuePair("messages", "{\"title\":\"hello\" ,\"description\": \"hello\"}"));
        nvps.add(new BasicNameValuePair("msg_keys", "android"));
        //nvps.add(new BasicNameValuePair("message_expires", ""));
        nvps.add(new BasicNameValuePair("timestamp",new Date().getTime()/1000+""));
        //nvps.add(new BasicNameValuePair("expires",""));
        //nvps.add(new BasicNameValuePair("v", "1"));
//        http://channel.api.duapp.com/rest/2.0/channel/channel?method=push_msg&
//        	apikey=Ljc710pzAa99GULCo8y48NvB&
//        	sign=8777F555E8C16715EBA5C85341684C58&
//        	timestamp=12344543232&
//        	expires=1238747373&
//        	v=1&
//        	push_type=1&
//        	channel_id=34r3fsdafsadf2f&
//        	user_id=34r3454y6578&
//        	messages=%E2%80%9Dtest%E2%80%9D&
//        	msg_keys=%E2%80%9Dtestkey%E2%80%9D
        
        
		try {
	        httpost.setEntity(new UrlEncodedFormEntity(nvps));
			HttpResponse response = httpclient.execute(httpost);
			HttpEntity entity = response.getEntity();
			if(entity!=null){
				String content = IOUtils.toString(entity.getContent(),"UTF-8");
				System.out.println(content);
			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		String address=MapApi.getAddressByLocation("121.452625,31.231694");
//		System.out.println(address);
	}
	
 
}
